﻿package com.peachcastle.tools 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.display.Scene;
	import flash.geom.Point;
	import flash.utils.getQualifiedClassName;
	
	public class Utils 
	{
		/**
		 * 从已有的对象中返回新的对象
		 * @param	obj
		 * @return
		 */
		public static function getNewObjectFromObject(obj:Object):*
		{
			return new obj.constructor();
		}
		
		/**
		 * 检测标签的有效性
		 * @param	clip		测试剪辑对象
		 * @param	frameLabel	帧标签名称
		 * @return	如果clip中存在frameLabel标签返回true，否则返回false
		 */
		public static function checkFrame(clip:MovieClip, frameLabel:String):Boolean		
		{
			var scene:Scene = clip.scenes[0];
			var labelList:Array = scene.labels;
			var label:FrameLabel = null;			
			for (var i:int = 0; i < labelList.length; i++)
			{
				label = labelList[i];
				if (label.name != frameLabel) continue;
				return true;
			}
			
			//
			return false;
		}
		
		/** 
		 * 获取当前时间
		 * @return
		 */
		public static function getCurrentTime():String
		{
			var now:Date = new Date();
			var hours:String = ((now.hours > 9)? "" : "0") + now.hours;
			var minutes:String = ((now.minutes > 9)? "" : "0") + now.minutes;
			return hours + ":" + minutes;
		}
		
		/**
		 * 打印树形显示列表
		 * @param	container
		 * @param	indent
		 */
		public static function treePrint(container:DisplayObjectContainer, indent:String = "",layerIndex:int = 1):void	
		{	
			for (var i:int = 0; i < container.numChildren;i++)
			{
				var child:DisplayObject = container.getChildAt(i);
				trace(indent + layerIndex +"\\-" + child.name + " --> " + child);		
				
				//
				if (child is DisplayObjectContainer)
				{				
					treePrint(child as DisplayObjectContainer, indent + "|\t", layerIndex + 1);				
				}
			}
		}
		
		/**
		 * 停止所有剪辑
		 * @param	clip
		 */
		public static function stopClip(clip:MovieClip):void
		{
			clip.stop();
			var child:DisplayObject = null;
			for (var i:int = 0; i < clip.numChildren; i++)
			{
				child = clip.getChildAt(i);
				if (child is MovieClip)
				{
					stopClip(child as MovieClip);
				}
			}
		}
		public static function playClip(clip:MovieClip):void
		{
			if (clip.totalFrames > 2) clip.play();
			var child:DisplayObject = null;
			
			for (var i:int = 0; i < clip.numChildren; i++)
			{
				child = clip.getChildAt(i);
				if (child is MovieClip)
				{
					playClip(child as MovieClip);
				}
			}
		}
		
		/**
		 * 获取对象所属类的名称
		 * @param	target			目标对象
		 * @param	isFullName		是否为完全限定名，例如：flash.events.Event
		 * @return
		 */
		public static function getClassName(target:Object, isFullName:Boolean = false):String		
		{
			var className:String = getQualifiedClassName(target);
			if(isFullName)
			{
				className = className.split("::").join(".");
				return className;
			}
			
			//
			className = className.replace(/.{1,}::(\w*)/, "$1");
			return className;
		}
		
		/**
		 * 拷贝数组
		 * @param	destination		目标数组
		 * @param	source			源数组
		 * @param	length			拷贝长度
		 * @param	dstOffset		目标数组偏移量
		 * @param	srcOffset		源数组偏移量
		 */
		public static function copyArray(destination:Array, source:Array, length:int,		
											dstOffset:int = 0,srcOffset:int = 0):void
		{
			if (source == null || destination == null) return;
			
			//
			if (length == 0 && source != null)	length = source.length;
			
			//
			for (var i:uint = 0; i < length; i++)			
			{
				destination[dstOffset + i] = source[srcOffset + i];				
			}
		}
		
		/**
		 * 拷贝数组
		 * @param	source	源数组
		 * @param	offset	偏移量
		 * @return	返回拷贝的数组对象
		 */
		public static function cloneArray(source:Array,offset:int):Array
		{
			return source.slice(offset);
		}
		
		/**
		 * 把子对象添加为父级对象容遮罩
		 * @param	childClip	当前对象的子对象
		 * @param	maskTarget	当前需要添加遮罩的显示对象
		 */
		public static function addChildAsMask(childClip:MovieClip, maskTarget:DisplayObjectContainer):void		
		{
			if (childClip == null) return;
			var point:Point = new Point(childClip.x, childClip.y);
			point = maskTarget.localToGlobal(point);
			point = maskTarget.parent.globalToLocal(point);
			
			//
			childClip.x = point.x;
			childClip.y = point.y;
			maskTarget.parent.addChild(childClip);
			
			//
			childClip.mouseEnabled = false;
			childClip.mouseChildren = false;
			childClip.alpha = 0;
			
			//
			maskTarget.mask = childClip;
		}
		
		/**
		 * 把对象的属性值与键值调换
		 * @param	target		目标对象
		 * @return
		 */
		public static function swapValue(target:Object):Object		
		{
			var result:Object = new Object();
			for (var i:String in target)
			{
				result["" + target[i]] = i;
			}
			
			//
			return result;
		}
		
		/**
		 * 从对象文本解析出对象
		 * 此方法可以剔除对象文本中包含的无效空格
		 * @param	objectString	对象文本
		 * @return
		 */
		public static function parseObject(objectString:String):Object
		{
			objectString = objectString.replace(/\{\s*|\s*\}|^\s*|\s*$/g, "");
			objectString = objectString.replace(/(,)\s+/g, "$1");			
			objectString = objectString.replace(/\s*(=|:|&)\s*/g, "$1");
			
			//
			var item:Array = null;
			var element:String = "";
			var result:Object = new Object();
			var list:Array = objectString.split(",");
			for (var i:int = 0; i < list.length; i++)
			{
				element = list[i];
				if (element == "") continue;
				
				//
				item = element.split(/=|:|&/);
				result["" + item[0]] = item[1];
			}
			
			//
			return result;
		}
		
		/**
		 * 把数字转换成以千分位分割的字符串
		 * @param	money	游戏币
		 * @return
		 */
		static public function dollarFormat(money:int):String
		{
			var str:String = money.toString();					
			var mode:int = str.length % 3;			
			var head:String = str.substr(0, mode);			
			if (str.length > 3 && str.length % 3 != 0)			
				head += ",";				
			str =  str.substr(mode);			
			str = str.replace(/\d{3}/g, "$&,");				
			str = "$ " + head + str.substr(0, str.length - 1);
			return str;
		}
		
		/**
		 * 返回灰度矩阵
		 * @return
		 */
		static public function getGrayMatrix():Array
		{
			var grayMatrix:Array = [];
			grayMatrix = grayMatrix.concat([0.2, 	0.2, 	0.2, 	0, 0]);
			grayMatrix = grayMatrix.concat([0.2, 	0.2, 	0.2, 	0, 0]);
			grayMatrix = grayMatrix.concat([0.2,	0.2, 	0.2, 	0, 0]);
			grayMatrix = grayMatrix.concat([0,	 	0, 		0, 		1, 0]);
			
			return grayMatrix;
		}
		
		/**
		 * 完全播放
		 * @param	clip
		 */
		public static function completePlay(clip:DisplayObjectContainer):void
		{
			var child:DisplayObject = null;
			for (var i:int = 0; i < clip.numChildren; i++)
			{
				child = clip.getChildAt(i);
				if(child is MovieClip)(child as MovieClip).play();
				
				if (child is DisplayObjectContainer)
					completePlay(child as DisplayObjectContainer);
			}
		}
		
		/**
		 * 完全停止
		 * @param	clip
		 */
		public static function completeStop(clip:DisplayObjectContainer):void
		{
			var child:DisplayObject = null;
			for (var i:int = 0; i < clip.numChildren; i++)
			{
				child = clip.getChildAt(i);
				if(child is MovieClip)(child as MovieClip).stop();
				
				//
				if (child is DisplayObjectContainer)
					completeStop(child as DisplayObjectContainer);
			}
		}
		
		/**
		 * 从Unicode码获取字符
		 * @param	codeString
		 */
		public static function getCharsFromCode(codeString:String):String
		{			
			var reg:RegExp = /%|\\u/g;
			var list:Array = codeString.split(reg);
			var output:String = "";
			for (var i:int = 0; i < list.length; i++)
			{
				var code:int = int("0x" + list[i]);	
				if (code == 0) continue;
				
				//
				output += String.fromCharCode(code);
			}
			
			//
			return output;
		}	
		
		/**
		 * 从包含有Unicode码的字符串获取正常的文本
		 * @param	codeString		包含有特殊字符的文本
		 * @param	output			输入对象，如果有输出，则存储在output["result"]变量里面
		 * @param	reg				正则对象，不需传参，函数自动处理
		 */
		public static function parseSpecialText(codeString:String, output:Object, reg:RegExp = null):void		
		{			
			//
			if (reg == null) reg = /(%[0-9A-F]{2})+/i;
			
			//
			var code:String = "";
			if (reg.test(codeString))code = Utils.getCharsFromCode(codeString.match(reg)[0]);			
			codeString = codeString.replace(reg, code);
			
			// 继续查询
			if (reg.test(codeString))
			{
				parseSpecialText(codeString, output, reg);
				return;
			}
			
			//
			output["result"] = codeString;
		}
		
		/**
		 *  产生m - n的随机数
		 * @param	min
		 * @param	max
		 * @return
		 */
		public static function randRange(min:Number, max:Number):Number {
			var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
			return randomNum;
		}
		/**
		 * 
		 * @param	ary
		 * @param	content
		 * @param	...param
		 * @return
		 */
		public static function getNameByArray(ary:Array, content:String,...param):Array
		{
			var data:Array = [];
			
			for each(var o:Object in ary)
			{
				for each(var p:String in param)
				{
					if (String(o[p]).indexOf(content) != -1)
					{
						if (data.indexOf(o) == -1) data[data.length] = o;
					}
					//trace(o[p]);
				}
			}
			
			return data;
		}
		

		public static function trim(s:String):String 
		{
			return s.replace(/^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm,"$2");
		}
	}
	
}